/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dao;
import myUtils.page.*;
import java.util.List;
import po.News;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
/**
 *
 * @author sony
 */
public class NewsDaoImpl extends HibernateDaoSupport implements NewsDao{

    public void save(News news) throws Exception
    {
        getHibernateTemplate().save(news);
    }

    public News getNews(int newsId) {
        return (News) getHibernateTemplate().get(News.class, newsId);
    }

    public void deleteNews(int newsId) throws Exception {
         Object p = getHibernateTemplate().load(News.class, newsId);
        this.getHibernateTemplate().delete(p);
    }

    public List getLatestNews(int n) {
        String queryString = "from News order by time desc";
        List temp = getHibernateTemplate().find(queryString);
        return temp.subList(0, Math.min(n,temp.size()));
    }

    public boolean isAuthor(int newsId, int userId) {
        String queryString = "from Userorgrelation as r where r.user.id="+userId
                +" and r.state=2 and r.organization.id in (Select o.id from Organization as o inner join o.newses as n with n.id="+newsId+")";
        return !getHibernateTemplate().find(queryString).isEmpty();
    }

    public void updateNews(News news) {
        this.getHibernateTemplate().update(news);
    }

    public Result getAllNews(int page) {
         String queryString = "from News order by time desc";
         return new Result(page,getHibernateTemplate().find(queryString));
    }

}
